#include "globals.h"
#include "menuscreen.h"
#include <iostream>
#include <list>
#include <string>


CMenuScreen::CMenuScreen(int id) {
	m_id = id;
	m_numberButtons = 0;
}

CMenuScreen::~CMenuScreen() {

}


std::list<CMenuScreen::menuButton*> CMenuScreen::getButtonList(void) {
	return menuButtonList;
}


void CMenuScreen::addButton(int order, int followScreenID, 
 std::string text, std::list<std::string> optionList, std::string curSetting) {
	LOG("CMenuScreen::addButton: screenID = " << m_id << " order = " << order <<
	" followScreenID = " << followScreenID << " text = " << text)

	menuButton *tmpMenuButton = new menuButton;
	tmpMenuButton->order = order;
	tmpMenuButton->followScreenID = followScreenID;
	tmpMenuButton->text = text;
	tmpMenuButton->availOptionsList = optionList;
	tmpMenuButton->currentSetting = curSetting;;

	if (optionList.size()==0) {
		LOG("CMenuScreen::addButton: optionList == NULL")
		tmpMenuButton->isOptionButton = 0;
	}
	else if (optionList.size() > 0) tmpMenuButton->isOptionButton = 1;
	if (curSetting.size()==0) LOG("CMenuScreen::addButton: defSetting == NULL")
	LOG("defaultSetting = " << curSetting)

	m_numberButtons++;
	menuButtonList.push_back(tmpMenuButton);
}

int CMenuScreen::getButtonFollowScreenID(menuButton *tmpMenuButton) {
	assert( tmpMenuButton );
	LOG("CMenuScreen::getButtonFollowScreenID: " << tmpMenuButton)
	return tmpMenuButton->followScreenID;
}

CMenuScreen::menuButton* CMenuScreen::detectCollision(int x, int y) {
	std::list<CMenuScreen::menuButton*>::iterator itMenuButton;

	//iterate through button list
	for (itMenuButton=menuButtonList.begin(); 
	 itMenuButton!=menuButtonList.end(); itMenuButton++) {


		//are coordinates located inside a button?
		if (x > (*itMenuButton)->x1 && x < (*itMenuButton)->x2 &&
		 y > (*itMenuButton)->y1 && y < (*itMenuButton)->y4){
			std::cout << "in zone" << std::endl;
			return *itMenuButton;
		}
	}
	return 0;
}

void CMenuScreen::cycleOptions(menuButton *menBut) {
	LOG("CMenuScreen::cycleOptions")

	//std::list<std::string>

	LOG(menBut->currentSetting)
	LOG(menBut->availOptionsList.size())
//	menBut->currentSetting = *menBut->availOptionsList.begin();
	LOG("menBut->currentSetting: " << menBut->currentSetting)

	//iterate through available options of this buttons
	std::list<std::string>::iterator itOptions;
	for (itOptions = menBut->availOptionsList.begin(); 
	 itOptions != menBut->availOptionsList.end();
	 itOptions++) {
		LOG("available options: " << *itOptions)

		//find options element that's currently set
		if ((*itOptions)==menBut->currentSetting) {
			//LOG("XX: currentSetting: " << *itOptions)
			//if incrementing the iterator hits the end
			//then start at the beginning again
			if ((++itOptions)==menBut->availOptionsList.end()) {
				itOptions = menBut->availOptionsList.begin();
			}
			//this is not needed because of itOptions is
			//already incremented above while checking
//			else itOptions++;

			menBut->currentSetting = *itOptions;

			LOG("XX: next Setting: " << *itOptions)

		}

	}

}
